package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"online/api/requset"
	"online/intrenal/dao"
	"online/intrenal/model"
)

func AddOnline(c *gin.Context) {
	var req requset.AddOnlineReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": nil,
		})
		return
	}
	result := dao.Create(&model.Online{
		UserId:    c.GetUint("userId"),
		Title:     req.Title,
		Desc:      req.Desc,
		Price:     req.Price,
		Inventory: req.Inventory,
	})
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "商品添加失败",
			"date": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "商品添加成功",
		"date": true,
	})
}

func UpdateOnline(c *gin.Context) {
	var req requset.UpdateOnlineReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": nil,
		})
		return
	}
	var online model.Online
	dao.GetOneById(req.Id, &online)
	if online.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "商品不存在",
			"date": nil,
		})
		return
	}
	if online.UserId != c.GetUint("userId") {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "没有修改权限",
			"date": nil,
		})
		return
	}
	result := dao.Update(&model.Online{
		Id:        req.Id,
		Desc:      req.Desc,
		Price:     req.Price,
		Inventory: req.Inventory,
	})
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "商品修改失败",
			"date": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "商品修改成功",
		"date": true,
	})
}

func ListOnline(c *gin.Context) {
	var req requset.ListOnlineReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": nil,
		})
		return
	}
	var online []model.Online
	dao.GetListByFields(&model.Online{
		Id:    req.Id,
		Title: req.Title,
	}, &online)
	//if !result {
	//	c.JSON(http.StatusOK, gin.H{
	//		"code": 10000,
	//		"msg":  "商品展示失败",
	//		"date": false,
	//	})
	//	return
	//}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "商品展示成功",
		"date": online,
	})
}

func PageOnline(c *gin.Context) {
	var req requset.PageOnlineReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": nil,
		})
		return
	}
	pageDto := dao.PageDto{
		Page:     req.Page,
		PageSize: req.PageSize,
		OrderBy:  "created_at desc",
	}
	var online []model.Online
	paginate, err := dao.Paginate(&model.Online{
		Id:    req.Id,
		Title: req.Title,
	}, pageDto, &online)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "商品分页展示失败",
			"date": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 10000,
		"msg":  "商品分页展示成功",
		"date": paginate,
	})

}

func ShowOnline(c *gin.Context) {
	var req requset.ShowOnlineReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": nil,
		})
		return
	}
	var online model.Online
	result := dao.GetOneById(req.Id, &online)
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "商品详情展示失败",
			"date": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "商品详情展示成功",
		"date": online,
	})
}
